var express = require('express');
var router = express.Router();
var config = require('../config');
var VandB = require('../db/VandB');
var adminDB = require('../db/adminDB');
var fs = require('fs');


router.get(/video|article|users|add/, function(req, res, next){
	try{
		if (req.session.admin.name == req.infos.name) {
			next()
		} else {
			res.redirect('/admin/login');
		}
	}catch(e){
		res.redirect('/admin/login');
	}
})

router.get('/login', function(req, res, next){
	res.render('./admin/adminLogin',{'title':'admin login','user':req.infos})
})

router.post('/login', function(req, res, next){
	var admin = {
		'name':req.body.name,
		'password':req.body.password
	};

	adminDB.findAdmin(req, admin.name, function(d){
		if (admin.password == d.password) {
			req.session.admin = {
				'id':d._id,
				'name':d.name,
				'password':d.password,
				'headImgUrl':d.headImgUrl,
				'motto':d.motto
			}
			res.redirect('/admin/video')
		} else {
			res.redirect('/admin/login')
		}
	})
})

router.post('/', function(req, res, next){
	up.upvideo(req, config.PATH, function(upPath){
		res.json(upPath)
	})
})

router.get('/ajax', function(req, res){
	res.json(req.session.jin)
})

router.get('/video', function(req, res, next){
	adminDB.findVandU(req, 'video', function(v){
		res.render('./admin/adminvideo',{
			'title':'视频管理',
			'user':req.infos,
			'video':v.dt
		})
	})
})

router.get('/article', function(req, res, next){
	adminDB.findVandU(req, 'article', function(a){
		res.render('./admin/adminarticle',{
			'title':'article管理',
			'user':req.infos,
			'article':a.dt
		})
	})
})

router.get('/users', function(req, res, next){
	var seal = req.query.seal;
	if (seal == null) {
		seal = 'seal';
	}
	var s;
	switch(seal){
		case 'seal':{
			s = true;
			break;
		}
		case 'unseal':{
			s = false;
			break;
		}
	}
	adminDB.findUserArr(req, s, function(u){
		res.render('./admin/adminuser',{
			'title':'users管理',
			'user':req.infos,
			'users':u.dt
		})
	})
})

router.get('/add', function(req, res, next){
	res.render('./admin/addAdmin', {'title':'add admin', 'user':req.infos});
})

router.get('/selectUserone', function(req, res, next){
	req.users.findById(req.query.id, function(err, result){
		if (err) {
			res.status(400).json(err);
		} else {
			res.status(200).json(result);
		}
	})
})

router.delete('/video', function(req, res, next){
	adminDB.delete(req, 'video', req.body.id, config.PATH+req.body.path, function(d){
		res.status(d.status).json(d.dt)
	})
})

router.put('/video', function(req, res, next){
	adminDB.pass(req, 'video', req.body.id, function(d){
		res.status(d.status).json(d.dt)
	})
})

router.delete('/article', function(req, res, next){
	adminDB.delete(req, 'article', req.body.id, config.BLOGPATH+req.body.img, function(d){
		res.status(d.status).json(d.dt)
	})
})

router.put('/article', function(req, res, next){
	adminDB.pass(req, 'article', req.body.id, function(d){
		res.status(d.status).json(d.dt)
	})
})

router.put('/article', function(req, res, next){
	adminDB.pass(req, 'article', req.body.id, function(d){
		res.status(d.status).json(d.dt)
	})
})

router.put('/users', function(req, res, next){
	var seal = req.query.seal;
	var newSeal;
	if (seal == 'seal') {
		newSeal = true;
	} else {
		newSeal = false;
	}
	adminDB.seal(req, req.body.id, newSeal, function(d){
		if (newSeal) {
			res.status(d.status).json({dt:'封印成功'})
			
		} else {
			res.status(d.status).json({dt:'封印解除'})
		}
	})
})

router.put('/addadmin', function(req, res, next){
	req.users.findById(req.body.id, function(err, result){
		if (err) {
			res.status(400).json(err)
		} else {
			var newAdmin = {
				name:result.name,
				password:result.password,
				email:result.email,
				LV:result.LV,
				headImgUrl:result.headImgUrl,
				motto:result.motto,
				sex:result.sex
			}
			adminDB.insertAdmin(req, newAdmin, function(d){
				res.status(d.status).json(d.dt);
			})
		}
	})
})

router.get('/carousel', function(req, res, next){
	var car = require('../car');
	res.render('./admin/admincarousel', {
		'title':'put carousel',
		'user':req.infos,
		'car':car
	});
	
})

router.put('/carousel', function(req, res, next){
	var newcar = req.body.newcar;

	fs.writeFile(config.dir+'/car.json', JSON.stringify(newcar), function(err){
		if (err) {
			console.log(err)
		} else {
			console.log('ssss')
		}
	})
})

module.exports = router;